<?php

namespace app\applet\controller;

use think\Cache;
use think\Config;
use think\Controller;
use think\exception\HttpResponseException;
use think\Response;
use think\View as ViewTemplate;

class Base extends Controller
{
    // 无需登陆的方法。小写方法名字
    protected $noNeedLogin = ['login'];
    protected $auth = null;
    protected $token;

    public function __construct()
    {
        parent::__construct();
        // auth赋值成对象
        $this->auth = new \stdClass();
        // 获取控制器名字
        $controllername = strtolower($this->request->controller());
        // 获取操作方法名字
        $actionname = strtolower($this->request->action());
        $this->token = input('token', '', 'trim,replace_dsyh');
        // 检测是否需要验证登录
        if ($this->token == '123') {
            $this->auth->id = 1;
        } else {
            //检测是否登录
            if (!in_array($actionname, $this->noNeedLogin)) {
                // 需要登陆。先获取缓存里用户信息
                $ca = Cache::get('applet_login');   // 二维数组
                if (!isset($ca[$this->token])) {
                    // 缓存里不存在这个token、表示伪造或者过期
                    $header['Access-Control-Allow-Origin'] = "*";
                    $header['Access-Control-Allow-Methods'] = "GET,POST";
                    Response::create(["code" => -99, "msg" => __("Please login first"), "data" => []], 'json', 200, $header)->send();
                    exit;
                }else{
                    // 将缓存里的用户信息放入auth对象
                    $this->auth = json_decode(json_encode($ca[$this->token]));
                }
            }
        }
    }


    /**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param mixed $msg 提示信息
     * @param string $url 跳转的 URL 地址
     * @param mixed $data 返回的数据
     * @param int $wait 跳转等待时间
     * @param array $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {

        $type = Config::get('default_ajax_return');
        $result = [
            'code' => 1,
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'wait' => $wait,
        ];

        if ('html' == strtolower($type)) {
            $template = Config::get('template');
            $view = Config::get('view_replace_str');

            $result = ViewTemplate::instance($template, $view)
                ->fetch(Config::get('dispatch_success_tmpl'), $result);
        }
        $header['Access-Control-Allow-Origin'] = "*";
        $header['Access-Control-Allow-Methods'] = "GET,POST";
        $response = Response::create($result, $type)->header($header);

        throw new HttpResponseException($response);
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param mixed $msg 提示信息
     * @param string $url 跳转的 URL 地址
     * @param mixed $data 返回的数据
     * @param int $wait 跳转等待时间
     * @param array $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        $type = Config::get('default_ajax_return');
        $result = [
            'code' => 0,
            'msg' => $msg,
            'data' => $data,
            'url' => $url,
            'wait' => $wait,
        ];

        if ('html' == strtolower($type)) {
            $template = Config::get('template');
            $view = Config::get('view_replace_str');

            $result = ViewTemplate::instance($template, $view)
                ->fetch(Config::get('dispatch_error_tmpl'), $result);
        }
        $header['Access-Control-Allow-Origin'] = "*";
        $header['Access-Control-Allow-Methods'] = "GET,POST";
        $response = Response::create($result, $type)->header($header);

        throw new HttpResponseException($response);
    }

}